package com.caoyuqi.picture.controll;


import com.caoyuqi.picture.service.CrudService;
import com.caoyuqi.picture.service.LoginService;
import com.caoyuqi.picture.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class LoginControll {

    @Autowired
    LoginService loginService;
    @Autowired
    CrudService crudService;
    @GetMapping("/")
    String LoginPage()
    {
        return "login";
    }

    @PostMapping("/adminPage")
    String Logincz(User user , HttpSession session ,Model model)
    {

        if(loginService.LoginSuccess(user.getUsername(),user.getPwd()))
        {
            String admin = crudService.queryAdmin(user.getUsername());

            if (admin.equals("false"))
            {

                session.setAttribute("username",user.getUsername());
                return "IndexPage";
            }

            if(admin.equals("true"))
            {
                ArrayList<com.caoyuqi.picture.model.User> users = loginService.QueryAll();
                model.addAttribute("users",users);
                session.setAttribute("username",user.getUsername());
                return "adminPage";
            }

        }else {
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }
        return "login";
    }
    @GetMapping("/adminPage")
    String adminPage(Model model)
    {
        ArrayList<com.caoyuqi.picture.model.User> users = loginService.QueryAll();
        model.addAttribute("users",users);
        return "adminPage";
    }

}
